package com.fyzk.activity.lottery.jc.football.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.App;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.newJz.JCZQ_ZJQ_Fragment;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.fyzk.bean.JCZQResponseInfo;
import com.fyzk.net.Constants;
import com.fyzk.utils.AppUtil;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by miao on 2017/7/15.
 */

public class JCZQ_ZJQ_Adapter extends BaseExpandableListAdapter {


    private Context context;
    private ArrayList<JCZQResponseInfo> infors;
    //    private List<String> betcodes;
    private boolean isDG;
    private int count;
    private JCZQ_ZJQ_Fragment jczq_zjq_fragment;


    private int maxCount = 10;
    private int index;

    public JCZQ_ZJQ_Adapter(Context context, int lotteryType, JCZQ_ZJQ_Fragment
            jczq_zjq_fragment, ArrayList<JCZQResponseInfo> infors) {
        this.context = context;
        this.infors = infors;
        this.jczq_zjq_fragment = jczq_zjq_fragment;
        if (App.isBaskOrder) {
            maxCount = 8;
        } else {
            maxCount = 10;
        }
    }

    public void setCount(int count) {
        this.count = count;
    }


    public void setInfos(List<JCZQResponseInfo> infos, int i) {

    }

    public void setDG(boolean DG) {
        this.isDG = DG;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return this.infors.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.infors.get(groupPosition).matchDatas.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.infors.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.infors.get(groupPosition).matchDatas.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition * 100 + childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
            parent) {
        JCZQResponseInfo info = infors.get(groupPosition);
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.jclq_adapter_group, null);
            groupHolder = new GroupHolder();
            groupHolder.date_tv = (TextView) convertView
                    .findViewById(R.id.jclq_group_tv);
            groupHolder.jianTou_iv = (ImageView) convertView
                    .findViewById(R.id.jclq_group_iv);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }

        groupHolder.date_tv.setText(info.matchTime + "   " + info.matchWeekday
                + "    共 " + info.matchDatas.size() + " 场");
        if (isExpanded) {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_shun));
        } else {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_ni));
        }

        return convertView;
    }

    class GroupHolder {
        TextView date_tv;
        ImageView jianTou_iv;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        index = childPosition;
        final JCInfo infor = infors.get(groupPosition).matchDatas.get(childPosition);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.buy_jczq_team_item_zjq,
                    null);
            holder = new ViewHolder();

            holder.xianShi = (LinearLayout) convertView.findViewById(R.id
                    .jc_main_list_item_xianshi);
            holder.ll_xiangqing = (LinearLayout) convertView.findViewById(R.id
                    .jc_main_list_item_yincang);
            holder.ivIsDanGuan = (ImageView) convertView.findViewById(R.id.ivIsDanGuan);
            holder.team = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_team);
            holder.num = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_num);
            holder.home = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_team_name1);
            holder.away = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_team_name2);
            holder.timeEnd = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_time_end);
            holder.jqs_0 = (CheckBox) convertView
                    .findViewById(R.id.zjq_team_item_0);
            holder.jqs_1 = (CheckBox) convertView
                    .findViewById(R.id.zjq_team_item_1);
            holder.jqs_2 = (CheckBox) convertView
                    .findViewById(R.id.zjq_team_item_2);
            holder.jqs_3 = (CheckBox) convertView
                    .findViewById(R.id.zjq_team_item_3);
            holder.jqs_4 = (CheckBox) convertView
                    .findViewById(R.id.zjq_team_item_4);
            holder.jqs_5 = (CheckBox) convertView
                    .findViewById(R.id.zjq_team_item_5);
            holder.jqs_6 = (CheckBox) convertView
                    .findViewById(R.id.zjq_team_item_6);
            holder.jqs_7 = (CheckBox) convertView
                    .findViewById(R.id.zjq_team_item_7);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.team.setBackgroundResource(AppUtil.getTeamNameColor(infor.getMatchName()));
        holder.team.setText(infor.getMatchName());
        holder.num.setText(infor.getMatchNum().substring(infor.getMatchNum().length() - 3) + "");

        holder.home.setText(infor.getHomeTeam());
        holder.away.setText(infor.getAwayTeam());


        holder.jqs_0.setText(getShowSP("0 " + infor.getScore_0()));
        holder.jqs_1.setText(getShowSP("1 " + infor.getScore_1()));
        holder.jqs_2.setText(getShowSP("2 " + infor.getScore_2()));
        holder.jqs_3.setText(getShowSP("3 " + infor.getScore_3()));
        holder.jqs_4.setText(getShowSP("4 " + infor.getScore_4()));
        holder.jqs_5.setText(getShowSP("5 " + infor.getScore_5()));
        holder.jqs_6.setText(getShowSP("6 " + infor.getScore_6()));
        holder.jqs_7.setText(getShowSP("7+ " + infor.getScore_7()));

        JCInfo.SPInfo SpScore0 = infor.btnHGPojo.get(infor.SCORE_0);
        JCInfo.SPInfo SpScore1 = infor.btnHGPojo.get(infor.SCORE_1);
        JCInfo.SPInfo SpScore2 = infor.btnHGPojo.get(infor.SCORE_2);
        JCInfo.SPInfo SpScore3 = infor.btnHGPojo.get(infor.SCORE_3);
        JCInfo.SPInfo SpScore4 = infor.btnHGPojo.get(infor.SCORE_4);
        JCInfo.SPInfo SpScore5 = infor.btnHGPojo.get(infor.SCORE_5);
        JCInfo.SPInfo SpScore6 = infor.btnHGPojo.get(infor.SCORE_6);
        JCInfo.SPInfo SpScore7 = infor.btnHGPojo.get(infor.SCORE_7);

        // 判断是否开售(赔率为空,禁止点击)
        holder.jqs_0.setChecked(SpScore0.isSel);
        holder.jqs_1.setChecked(SpScore1.isSel);
        holder.jqs_2.setChecked(SpScore2.isSel);
        holder.jqs_3.setChecked(SpScore3.isSel);
        holder.jqs_4.setChecked(SpScore4.isSel);
        holder.jqs_5.setChecked(SpScore5.isSel);
        holder.jqs_6.setChecked(SpScore6.isSel);
        holder.jqs_7.setChecked(SpScore7.isSel);
        SimpleDateFormat format_ = new SimpleDateFormat("HH:mm");
        holder.timeEnd.setText(format_.format(new Date(infor.getEndSaleTime())) + "[截]");

        onclikBtn();

        holder.jqs_0.setTag(SpScore0);
        holder.jqs_1.setTag(SpScore1);
        holder.jqs_2.setTag(SpScore2);
        holder.jqs_3.setTag(SpScore3);
        holder.jqs_4.setTag(SpScore4);
        holder.jqs_5.setTag(SpScore5);
        holder.jqs_6.setTag(SpScore6);
        holder.jqs_7.setTag(SpScore7);

        holder.jqs_0.setTag(R.string.jc_info, infor);
        holder.jqs_1.setTag(R.string.jc_info, infor);
        holder.jqs_2.setTag(R.string.jc_info, infor);
        holder.jqs_3.setTag(R.string.jc_info, infor);
        holder.jqs_4.setTag(R.string.jc_info, infor);
        holder.jqs_5.setTag(R.string.jc_info, infor);
        holder.jqs_6.setTag(R.string.jc_info, infor);
        holder.jqs_7.setTag(R.string.jc_info, infor);


        holder.jqs_0.setOnClickListener(scoreClickListener);
        holder.jqs_1.setOnClickListener(scoreClickListener);
        holder.jqs_2.setOnClickListener(scoreClickListener);
        holder.jqs_3.setOnClickListener(scoreClickListener);
        holder.jqs_4.setOnClickListener(scoreClickListener);
        holder.jqs_5.setOnClickListener(scoreClickListener);
        holder.jqs_6.setOnClickListener(scoreClickListener);
        holder.jqs_7.setOnClickListener(scoreClickListener);

        if (infor.isShowHideView) {
            holder.ll_xiangqing.setVisibility(View.VISIBLE);
        } else {
            holder.ll_xiangqing.setVisibility(View.GONE);
        }
        if (infor.hideView != null) {

            ViewGroup parent1 = (ViewGroup) infor.hideView.getParent();
            if (parent1 != null) {
                parent1.removeAllViews();
            }
            holder.ll_xiangqing.removeAllViews();
            holder.ll_xiangqing.addView(infor.hideView);
        }

        // 点击显示隐藏信息
        holder.xianShi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (infor.hideView == null) {
//                    infor.hideView = new FootballMatchSummaryView(context, infor.getMatchNum());
//                    holder.ll_xiangqing.removeAllViews();
//                    holder.ll_xiangqing.addView(infor.hideView);
//                }
//                infor.isShowHideView = !infor.isShowHideView;
//                if (infor.isShowHideView) {
//                    holder.ll_xiangqing.setVisibility(View.VISIBLE);
//                } else {
//                    holder.ll_xiangqing.setVisibility(View.GONE);
//                }
                Intent intent = new Intent(context, NewScoreDetailActivity.class);
                intent.putExtra("isFootball", true);
                intent.putExtra("matchNum", infor.getMatchNum());
                context.startActivity(intent);
            }

        });

//        holder.ll_xiangqing.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                // onClick1(JCZQ_BF_Activity.WHAT_FENXI, position);
//                Intent intent = new Intent(context, ScoreDetailActivity.class);
//                intent.putExtra("matchNum", infor.getMatchNum());
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }


    View.OnClickListener scoreClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onScoreClick(v);
        }
    };

    private void onScoreClick(View view) {
        JCInfo.SPInfo spInfo = (JCInfo.SPInfo) view.getTag();
        JCInfo infor = (JCInfo) view.getTag(R.string.jc_info);
        if (infor.getOnclikNum() == 0 && count == maxCount) {
            AppToast.getToast().show("最多选择" + maxCount + "场比赛");
            spInfo.isSel = (false);
            JCZQ_ZJQ_Adapter.this.notifyDataSetChanged();
            return;
        } else {
            boolean isFail = spInfo.isSel;
            spInfo.isSel = !isFail;
            if (spInfo.isSel == true) {
                infor.setOnclikNum(infor.getOnclikNum() + 1);
            } else {
                infor.setOnclikNum(infor.getOnclikNum() - 1);
            }
            onclikBtn();
        }
    }


    private SpannableString getShowSP(String CONTENT) {
        RelativeSizeSpan span = new RelativeSizeSpan(Constants.scaleJCTxt);
        SpannableString spannableString = new SpannableString(CONTENT);
        spannableString.setSpan(span, 0, 2, Spanned
                .SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    class ViewHolder {
        LinearLayout xianShi;
        LinearLayout ll_xiangqing;

        ImageView ivIsDanGuan;

        TextView team;
        TextView num;
        TextView home;
        TextView away;
        TextView timeEnd;

        CheckBox jqs_0;
        CheckBox jqs_1;
        CheckBox jqs_2;
        CheckBox jqs_3;
        CheckBox jqs_4;
        CheckBox jqs_5;
        CheckBox jqs_6;
        CheckBox jqs_7;
    }

    public void onclikBtn() {
        jczq_zjq_fragment.adapterChange(infors);
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}